﻿<template>
  <div class="app-container">
    <div class="filter-title">
      <el-form label-position="right" label-width="80px">
        <el-form-item :label="$t('maintenance.name')+' :'">
          <el-input
            v-model="listQuery.name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('maintenance.name')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.mobile')+' :'">
          <el-input
            v-model="listQuery.mobile"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('public.mobile')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('maintenance.sex')+' :'">
          <el-select
            v-model="listQuery.sex"
            clearable
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('maintenance.sex')"
          >
            <el-option
              v-for="item in sexOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >{{ $t('table.search') }}</el-button>
        <el-button
          v-if="permission.add"
          style="margin-left: 10px;float: right"
          class="filter-item"
          type="primary"
          icon="el-icon-edit"
          @click="handleCreate"
        >{{ $t('table.add') }}</el-button>
        <el-button
          style="margin-left: 10px"
          class="filter-item"
          type="primary"
          @click="handleRefresh"
        >{{ $t('table.refresh') }}</el-button>
        <div v-show="!showSearchItem" class="showDown" @click="showSearchItem = true">
          <i class="el-icon-d-arrow-right"></i>
        </div>
        <div v-show="showSearchItem" class="showUp" @click="showSearchItem = false">
          <i class="el-icon-d-arrow-left"></i>
        </div>
      </el-form>
    </div>
    <div v-show="showSearchItem" class="filter-container">
      <el-form label-position="right" label-width="80px">
        <el-form-item :label="$t('maintenance.startAge')+' :'">
          <el-input
            v-model="listQuery.startAge"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('maintenance.startAge')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('maintenance.endAge')+' :'">
          <el-input
            v-model="listQuery.endAge"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('maintenance.endAge')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.status')+' :'">
          <el-select
            v-model="listQuery.status"
            clearable
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('public.status')"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255,255,255,0.2)"
      border
      fit
      highlight-current-row
      style="width: 100%min-height:1000px"
    >
      <el-table-column fixed="left" align="center" :label="$t('table.id')" width="65">
        <template slot-scope="scope">
          <span>{{ (listQuery.page-1)*listQuery.limit+scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('maintenance.name')">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('public.mobile')">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('maintenance.age')">
        <template slot-scope="scope">
          <span>{{ scope.row.age }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('maintenance.sex')">
        <template slot-scope="scope">
          <span>{{ sexEs(scope.row.sex) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('maintenance.birthTime')">
        <template slot-scope="scope">
          <span>{{ scope.row.birth_time }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        show-overflow-tooltip
        :label="$t('maintenance.skillDescribe')"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.skill_describe }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('maintenance.malfunctionNumber')">
        <template slot-scope="scope">
          <span>{{ scope.row.malfunction_number }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('maintenance.comment')">
        <template slot-scope="scope">
          <span>{{ scope.row.comment }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('public.status')">
        <template slot-scope="scope">
          <span :class="statusColor(scope.row.status)">{{ leaveEs(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('public.createTime')">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        :label="$t('table.actions')"
        :width="operWidth"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleRead(scope.row)"
          >{{ $t('table.read') }}</el-button>
          <el-button
            v-if="permission.update"
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >{{ $t('table.edit') }}</el-button>
          <el-button
            v-if="scope.row.status === 0 && permission.del"
            type="danger"
            size="mini"
            @click="handleDisable(scope.row)"
          >{{ $t('table.disable') }}</el-button>
          <el-button
            v-else-if="permission.del"
            type="success"
            size="mini"
            @click="handleEnable(scope.row)"
          >{{ $t('table.enable') }}</el-button>
          <el-button
            v-if="permission.del"
            size="mini"
            type="danger"
            @click="handleModifyStatus(scope.row,'deleted')"
          >{{ $t('table.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
    <!-- 编辑 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        label-width="35%"
        style="width: 400px margin-left:120px"
        size="small"
      >
        <el-form-item :label="$t('maintenance.name')+' :'" prop="name">
          <el-input v-model="temp.name"></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.mobile')+' :'" prop="mobile">
          <el-input v-model="temp.mobile"></el-input>
        </el-form-item>
        <el-form-item :label="$t('maintenance.age')+' :'" prop="age">
          <el-input-number v-model="temp.age" :min="1" :max="150"></el-input-number>
        </el-form-item>
        <el-form-item :label="$t('maintenance.sex')+' :'" prop="sex">
          <el-radio-group v-model="temp.sex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="dialogStatus!='create'"
          :label="$t('maintenance.malfunctionNumber')+' :'"
        >
          <span>{{ temp.malfunction_number }}</span>
        </el-form-item>
        <el-form-item v-if="dialogStatus!='create'" :label="$t('maintenance.comment')+' :'">
          <span>{{ temp.comment }}</span>
        </el-form-item>
        <el-form-item :label="$t('maintenance.skillDescribe')+' :'" prop="skill_describe">
          <el-input v-model="temp.skill_describe" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button
          v-if="dialogStatus=='create'"
          type="primary"
          :disabled="!createDisabled"
          @click="createData"
        >{{ $t('table.confirm') }}</el-button>
        <el-button
          v-else
          type="primary"
          :disabled="!updateDisabled"
          @click="updateData"
        >{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>

    <!-- 查看 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormRead">
      <el-form
        :model="temp"
        label-position="right"
        label-width="40%"
        style="width: 400px margin-left:120px"
      >
        <el-form-item :label="$t('maintenance.name')+' :'">
          <span>{{ temp.name }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.mobile')+' :'">
          <span>{{ temp.mobile }}</span>
        </el-form-item>
        <el-form-item :label="$t('maintenance.age')+' :'">
          <span>{{ temp.age }}</span>
        </el-form-item>
        <el-form-item :label="$t('maintenance.sex')+' :'">
          <span>{{ sexEs(temp.sex) }}</span>
        </el-form-item>
        <el-form-item
          v-if="dialogStatus!='create'"
          :label="$t('maintenance.malfunctionNumber')+' :'"
        >
          <span>{{ temp.malfunction_number }}</span>
        </el-form-item>
        <el-form-item v-if="dialogStatus!='create'" :label="$t('maintenance.comment')+' :'">
          <span>{{ temp.comment }}</span>
        </el-form-item>
        <el-form-item :label="$t('maintenance.skillDescribe')+' :'">
          <span>{{ temp.skill_describe }}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "Maintenance",
  data() {
    const name = (rule, value, callback) => {
      // return false
      if (value.length > 0) {
        if (value.length > 16) {
          callback(new Error("名称长度不能大于16个字符"));
        } else {
          var reg = /^[\u4e00-\u9fffa-z-A-Z]+$/; /* 定义验证表达式 */
          if (reg.test(value)) {
            /* 进行验证 */
            callback();
          } else {
            callback(new Error("姓名只能包含汉字、英文"));
          }
        }
      } else {
        callback(new Error("请填写姓名"));
      }
    };
    const mobile = (rule, value, callback) => {
      // return false
      if (value.length > 0) {
        var reg = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/; /* 定义验证表达式 */
        if (reg.test(value)) {
          /* 进行验证 */
          callback();
        } else {
          callback(new Error("请输入正确的联系电话"));
        }
      } else {
        callback(new Error("请填写联系电话"));
      }
    };
    const describe = (rule, value, callback) => {
      // return false
      if (value.length > 200) {
        callback(new Error("技能描述不得超过200个字符"));
      } else {
        callback();
      }
    };
    return {
      showSearchItem: false,
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      statusOptions: [
        {
          value: 0,
          label: "在岗"
        },
        {
          value: 1,
          label: "离职"
        }
      ],
      sexOptions: [
        {
          value: 1,
          label: "男"
        },
        {
          value: 2,
          label: "女"
        }
      ],
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined,
        name: "",
        mobile: "",
        startAge: "",
        endAge: "",
        sex: "",
        status: ""
      },
      temp: {
        id: undefined,
        name: "",
        mobile: "",
        age: "",
        sex: "",
        birth_time: "",
        skill_describe: "",
        status: ""
      },
      dialogFormVisible: false,
      dialogFormRead: false,
      dialogStatus: "",
      textMap: {
        update: "更新",
        create: "添加",
        review: "审核",
        read: "查看"
      },
      rules: {
        name: [{ required: true, trigger: "blur", validator: name }],
        mobile: [{ required: true, trigger: "blur", validator: mobile }],
        age: [{ required: true, message: "请填写年龄", trigger: "blur" }],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],
        status: [{ required: true, message: "请审核", trigger: "change" }],
        skill_describe: [{ trigger: "blur", validator: describe }]
      },
      createDisabled: true,
      updateDisabled: true,
      loading: false,
      industryOptions: [],
      areaOptions: [],
      permission: {
        add: false,
        del: false,
        update: false
      }
    };
  },
  computed: {
    operWidth: function() {
      let itemCount = 1;
      if (this.permission.del) {
        itemCount += 2;
      }
      if (this.permission.update) {
        itemCount += 1;
      }
      if (this.permission.review) {
        itemCount += 1;
      }
      return itemCount * 80 + "px";
    }
  },
  created() {
    if (this.$store.state.permission.jurisdiction === "*") {
      this.permission.add = true;
      this.permission.update = true;
      this.permission.del = true;
    } else {
      const jurisdiction = this.$store.state.permission.jurisdiction.split(",");
      jurisdiction.forEach(element => {
        const id = parseInt(element);
        if (id === 144) {
          this.permission.add = true;
        }
        if (id === 145) {
          this.permission.del = true;
        } else if (id === 146) {
          this.permission.update = true;
        }
      });
    }
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page
      };
      if (this.listQuery.name) {
        data["name"] = this.listQuery.name;
      }
      if (this.listQuery.mobile) {
        data["mobile"] = this.listQuery.mobile;
      }
      if (this.listQuery.startAge) {
        data["startAge"] = this.listQuery.startAge;
      }
      if (this.listQuery.endAge) {
        data["endAge"] = this.listQuery.endAge;
      }
      if (this.listQuery.sex) {
        data["sex"] = this.listQuery.sex;
      }
      if (this.listQuery.status || this.listQuery.status === 0) {
        data["status"] = this.listQuery.status;
      }
      const maintainIndexPath = {
        // 获取维护人员列表
        company: "STQ",
        api: "pc_Maintain_index",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(maintainIndexPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleModifyStatus(row) {
      this.$confirm("此操作将永久删除该维护人员信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const maintainDeletePath = {
            company: "STQ",
            api: "pc_Maintain_delete",
            version: "1.0",
            data: {
              id: row.id
            }
          };
          this.socketApi.sendSock(JSON.stringify(maintainDeletePath), res => {
            if (res) {
              this.socketData(res);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        name: "",
        mobile: "",
        age: "",
        sex: "",
        birth_time: "",
        skill_describe: "",
        status: ""
      };
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        id: undefined,
        name: "",
        mobile: "",
        startAge: "",
        endAge: "",
        sex: "",
        status: ""
      };
      this.getList();
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    createData() {
      // 添加维护人员
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          if (this.createDisabled) {
            this.createDisabled = false;
            const data = {
              name: this.temp.name,
              mobile: this.temp.mobile,
              age: this.temp.age,
              sex: this.temp.sex
            };
            if (this.temp.skill_describe) {
              data["skill_describe"] = this.temp.skill_describe;
            }
            const maintainAddPath = {
              company: "STQ",
              api: "pc_Maintain_add",
              version: "1.0",
              data: data
            };
            this.socketApi.sendSock(JSON.stringify(maintainAddPath), res => {
              if (res) {
                this.socketData(res);
              }
              setTimeout(() => {
                this.createDisabled = true;
              }, 1000);
            });
          }
        }
      });
    },
    handleUpdate(row) {
      // 打开编辑弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.temp.region_id = parseInt(this.temp.region_id);
      this.temp.trade_ids = parseInt(this.temp.trade_ids);
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.getIndustry();
      this.getArea();
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleRead(row) {
      // 打开查看弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "read";
      this.dialogFormRead = true;
    },
    handleDisable(row) {
      const maintainEditPath = {
        company: "STQ",
        api: "pc_Maintain_edit",
        version: "1.0",
        data: {
          id: row.id,
          status: 1
        }
      };
      this.socketApi.sendSock(JSON.stringify(maintainEditPath), res => {
        if (res) {
          this.socketData(res);
        } else {
          this.$message.error(" 启用失败 ");
        }
        setTimeout(() => {
          this.updateDisabled = true;
        }, 1000);
      });
    },
    handleEnable(row) {
      const maintainEditPath = {
        company: "STQ",
        api: "pc_Maintain_edit",
        version: "1.0",
        data: {
          id: row.id,
          status: 0
        }
      };
      this.socketApi.sendSock(JSON.stringify(maintainEditPath), res => {
        if (res) {
          this.socketData(res);
        } else {
          this.$message.error(" 禁用失败 ");
        }
        setTimeout(() => {
          this.updateDisabled = true;
        }, 1000);
      });
    },
    updateData() {
      // 更新维护人员信息
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          if (this.updateDisabled) {
            this.updateDisabled = false;
            const data = {
              id: this.temp.id,
              name: this.temp.name,
              mobile: this.temp.mobile,
              age: this.temp.age,
              sex: this.temp.sex
            };
            if (this.temp.skill_describe) {
              data["skill_describe"] = this.temp.skill_describe;
            }
            const maintainEditPath = {
              company: "STQ",
              api: "pc_Maintain_edit",
              version: "1.0",
              data: data
            };
            this.socketApi.sendSock(JSON.stringify(maintainEditPath), res => {
              if (res) {
                this.socketData(res);
              }
              setTimeout(() => {
                this.updateDisabled = true;
              }, 1000);
            });
          }
        }
      });
    },
    // 区域行业选择
    getIndustry(query) {
      const industryIndexPath = {
        company: "STQ",
        api: "pc_trade_index",
        version: "1.0",
        data: {
          name: query
        }
      };
      this.socketApi.sendSock(JSON.stringify(industryIndexPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    getArea(query) {
      const areaIndexPath = {
        company: "STQ",
        api: "pc_region_index",
        version: "1.0",
        data: {
          name: query
        }
      };
      this.socketApi.sendSock(JSON.stringify(areaIndexPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "pc_region_index") {
            this.areaOptions = resj.data;
          } else if (resj.api === "pc_trade_index") {
            this.industryOptions = resj.data;
          } else if (resj.api === "pc_Maintain_index") {
            this.total = resj.count;
            this.list = resj.data;
            this.listLoading = false;
          } else if (resj.api === "pc_Maintain_add") {
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: "成功",
              message: "添加成功",
              type: "success",
              duration: 2000
            });
          } else if (resj.api === "pc_Maintain_edit") {
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: "成功",
              message: "更新成功",
              type: "success",
              duration: 2000
            });
          } else if (resj.api === "pc_Maintain_delete") {
            this.getList();
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        }
      }
    }
  }
};
</script>
